<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>开店管家</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<style type="text/css">
			html,body{
				width: 100%;
				height: 100%;
				background-color: #f3f4f6;
			}
			.container{
				display: flex;
				justify-content: center;
				align-items: center;
				padding-top:.94rem;
				height: 2rem;
				background-color: #FFFFFF;
			}
			#pay-state{
				display: inline-block;
				width: 1rem;
				height: 1rem;
				background-repeat: no-repeat;
				background-position: center;
				background-size: 70% 70%;
			}
			#pay-result{
				height: 1rem;
				line-height: 1rem;
				font-size: .45rem;
				padding-left: .15rem;
			}
			.wait{
				background-image: url(img/wait.png);
			}
			.success{
				background-image: url(img/success.png);
			}
			.failure{
				background-image: url(img/failure.png);
			}
			#pay-info{
				height: .6rem;
				line-height: .6rem;
				padding-bottom: .5rem;
				font-size: .35rem;
				text-align: center;
				background-color: #FFFFFF;
				color: #a7a7a7;
			}
			#ad{
				height: 1.4rem;
				width: 100%;
				position: fixed;
				bottom: 0;
				left: 0;
			}
		</style>
		<!--jquery-->
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
		 <!--解决ios延迟问题 -->
		<script src="./js/fastclick.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">$(function() {FastClick.attach(document.body);});</script>
		<!--全局管理-->
		<script src="js/base.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		
		<div class="container">
			<span id="pay-state" class="wait"></span><span id="pay-result">处理中……</span>
		</div>
		<div id="pay-info"></div>
		<div id="ad">
			<a id="ad-a" href="https://interaction.bayimob.com/gameHtml?appkey=31df29e7b75ed320bdf865b800e7ade9&adSpaceKey=ab70d3de93a199786572102ceaf23170&1=1">
				<img width="100%" height="100%" src="./img/ad.png"/>
			</a>
		</div>
		<script type="text/javascript">
		// 自适应字体大小
			(function () {
				var designW = 750;  //设计稿宽
				var font_rate = 100;
				//适配
				document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
				document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
		
				//监测窗口大小变化
				window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function () {
					document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
					document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
				}, false);
			})();		
		</script>
		<script type="text/javascript">
			let code = GetQueryString('code');
			let data = GetQueryString('data');
			let amount = data.split('-')[0];
			let platformCode = data.split('-')[1];;
			let channel = "wechat";
			let baseUrl = window.baseUrl;	
			$('#ad-a').attr('href',window.adUrl);
//			$('#code').html( "code为："+ code + "\n amount为" + amount + "\n platformCode为"+ platformCode + "\n channel为"+channel );
			$('#pay-info').html("本次消费"+amount+"元");
			$.ajax({
				type:"post",
				url:baseUrl+"paymentchannel/order/create",
				data:{
					amount,
					platformCode,
					channel,
					code
				},
				success:function(res){
//					alert(JSON.stringify(res));
					if(res.resp_code === "000000"){
						handleWechat(res.result);
					}else{
						alert(res.resp_message);
					}
				},
				error:function(err){
					alert("微信发起支付失败")
				}
			});
			
			function handleWechat(result){
				
				let option =  {
			        "timeStamp": result.timeStamp,
			        "package": result.package,
			        "paySign": result.paySign,
			        "appId": result.appId,
			        "signType": result.signType,
			        "nonceStr": result.nonceStr
			    }
								
				// 添加js桥
				if (typeof WeixinJSBridge == "undefined"){
				   if( document.addEventListener ){
				       document.addEventListener('WeixinJSBridgeReady', function(){onBridgeReady(option)}, false);
				   }else if (document.attachEvent){
				       document.attachEvent('WeixinJSBridgeReady', function(){onBridgeReady(option)}); 
				       document.attachEvent('onWeixinJSBridgeReady', function(){onBridgeReady(option)});
				   }
				 
				}else{
				   onBridgeReady(option);
				}
			}
			
			function GetQueryString(name){
			    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			    var r = window.location.search.substr(1).match(reg);
			    if(r!=null)return  unescape(r[2]);
			    return null;
			}
			
			function onBridgeReady(option){
			   WeixinJSBridge.invoke(
			      'getBrandWCPayRequest',option,
			      function(res){
			      WeixinJSBridge.log(res.err_msg);
//				 alert(JSON.stringify(res));
			      if(res.err_msg == "get_brand_wcpay_request:ok" ){
			      		$("#pay-result").html("支付成功！");
			      		$("#pay-state").addClass("success");
			      		window.location.href = window.adUrl;
			      		// 使用以上方式判断前端返回,微信团队郑重提示：
			            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
			      } 
			   }); 
			}
		</script>
	</body>
</html>
